package fr.persistence.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import fr.form.User;
import fr.persistence.ManageUserPersistenceService;

@Service
public class ManageUserPersistenceServiceImpl 
	extends GenericDaoImpl<User, Integer> 
	implements ManageUserPersistenceService{

    @Autowired
	private SessionFactory sessionFactory;

	public ManageUserPersistenceServiceImpl() {
		super(User.class);
	}

	public ManageUserPersistenceServiceImpl(Class<User> type) {
		super(type);
	}

	@SuppressWarnings("unchecked")
	public List<User> getUserByMail(String pMail) {

		Session s = sessionFactory.getCurrentSession();
		List<User> users;
		
		Criteria criteres = s.createCriteria(User.class);
			
		if((pMail != null) && (!pMail.isEmpty())){
			criteres.add(Restrictions.eq("mail",pMail));
		}
			
		users = (List<User>) criteres.list();
		return users;
	}

}
